﻿using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using OS.Common.Plugs.ConfigPlug;

namespace ConsoleApp1
{
    public class ConfigurationUtil : IConfigPlug
    {
        private static IConfiguration config;

        public static IConfiguration Configuration
        {
            get
            {
                if (config != null) return config;
                var configBuilder = new ConfigurationBuilder().Add(new JsonConfigurationSource()
                {
                    Path = "appsettings.json",
                    ReloadOnChange = true
                });
                config = configBuilder.Build();
                return config;
            }
            set => config = value;
        }

        public IConfigurationSection GetSection(string key)
        {
            return Configuration?.GetSection(key);
        }

        public string GetConnectionString(string name)
        {
            return Configuration?.GetConnectionString(name);
        }

        public IEnumerable<IConfigurationSection> GetChildren()
        {
            return Configuration?.GetChildren();
        }
    }
}